class ItemCategory < ActiveRecord::Base
  belongs_to :parent, :class_name => 'ItemCategory', :foreign_key => 'parent_id'
  has_many :children, :class_name => 'ItemCategory', :foreign_key => 'parent_id', :order => :sort_order
  has_many :cat1_items, :class_name => 'Item', :foreign_key => :cat1_id
  has_many :cat2_items, :class_name => 'Item', :foreign_key => :cat2_id
    
  #attr_accessible :name, :parent_id, :cat_level, :is_parent, :sort_order, :status, :flag, :permalink
  
  validates_presence_of :name
  validates_uniqueness_of :name
  validates_numericality_of :cat_level
  
  named_scope :valid, :conditions => ['status >= ?', 0]
  named_scope :cat1, :conditions => ["parent_id IS ? AND status = ?", nil, 0], :order => :sort_order
  named_scope :cat2, :conditions => ["parent_id IS NOT ? AND status = ?", nil, 0], :order => :sort_order
  
  def to_s
    name
  end
  
  def del
    update_attributes!(:status => -1)
    children.update_all("status = '-1'")
  end
end
